package com.dkd.flow.strategy.service.operation;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSONObject;
import com.dkd.common.core.domain.entity.SysUser;
import com.dkd.flow.context.ProcessContext;
import com.dkd.flow.context.ProcessContextBuilder;
import com.dkd.flow.domain.EProcessInstances;
import com.dkd.flow.domain.EProcessTask;
import com.dkd.flow.emums.FlowOperation;
import com.dkd.flow.emums.FlowStatus;
import com.dkd.flow.emums.TaskActionType;
import com.dkd.flow.emums.TaskStatus;
import com.dkd.flow.service.impl.EProcessTaskServiceImpl;
import com.dkd.flow.strategy.annotation.Strategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Strategy(FlowOperation.RE_SUBMIT_ALL)
public class ReSubmitAllStrategy extends BaseStrategy {
    @Autowired
    ProcessContextBuilder processContextBuilder;
    @Autowired
    private EProcessTaskServiceImpl eProcessTaskServiceImpl;
    @Override
    public Long execute(JSONObject data) {
        // 使用构建器初始化上下文 --- start --- //
        ProcessContext context = processContextBuilder.build(data);
        EProcessInstances eProcessInstances = context.getEProcessInstances();
        EProcessTask curTask = context.getCurTask();
        Long curTaskStatus = context.getCurTaskStatus();
        EProcessInstances paramsEProcessInstances = context.getEProcessInstances();
        JSONObject formValue = context.getFormValue();
        Long processDefinitionId = context.getProcessDefinitionId();
        Long curTaskId = context.getCurTaskId();
        // 使用构建器初始化上下文 --- end --- //
        Long taskGroupId = taskCreate.getTaskGroupId(eProcessInstances);
        initNodeRelations(processDefinitionId);
        //创建发起人重新发起的任务
        JSONObject launchNode = links.get(0)
                .getJSONObject("toNode");
        if (launchNode == null) {
            throw new RuntimeException("links 中第一个节点的 toNode 为 null，无法创建发起人任务");
        }
        SysUser liableUser = nodeUtils.getLiableUser(eProcessInstances);
        //创建	发起人任务 INITIATOR_TASK 1	自己	重新提交所有 RE_SUBMIT_ALL 10	处理中 PROCESSING 1
        Long launchTaskId = IdUtil.getSnowflakeNextId();
        EProcessTask launchTask = taskCreate.createLaunchTask(eProcessInstances,
                                                              launchNode,
                                                              null,
                                                              TaskStatus.RE_SUBMIT_ALL,
                                                              launchTaskId,
                                                              null,
                                                              launchTaskId,
                                                              liableUser,
                                                              taskGroupId,
                                                              JSONObject.toJSONString(formValue),
                                                              TaskActionType.OPERATION_TASK
        );
        //创建	审批人任务 APPROVER_TASK 2	下一个	审批中 UNDER_APPROVAL 2	处理中 PROCESSING 1
        //创建	审批人任务 APPROVER_TASK 2	并行	审批中 UNDER_APPROVAL 2	处理中 PROCESSING 1
        String nextProcessDesignId = handlerNextNode(
                launchTask.getProcessDesignId(),
                formValue,
                eProcessInstances,
                links,
                curTask,
                taskGroupId,
                TaskActionType.LOG_TASK,
                launchTaskId
        );
        //重置审批节点的审批数量
        resetEProcessInstancesApproverCount(eProcessInstances,
                                            nextProcessDesignId,
                                            0);
        //实例状态
        eProcessInstances.setFlowStatus(FlowStatus.PROCESSING.getValue()
                                                .longValue());
        updateEProcessInstances(eProcessInstances);
        //清除操作组ID
        taskCreate.clearOperationGroupId(eProcessInstances);
        return launchTask.getID();
    }
}